#!/bin/bash

# Exit on any error
set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Temporary log file until user is created
TEMP_LOG="/tmp/acis_setup_temp.log"
> "$TEMP_LOG"

# Function to display header
display_header() {
    clear
    echo -e "${BLUE}"
    cat <<'EOF'
 _____         _  _          ______                                  
/  ___|       | || |         |  _  \                                 
\ `--.   __ _ | || |_  _   _ | | | | _ __  __ _   __ _   ___   _ __  
 `--. \ / _` || || __|| | | || | | || '__|/ _` | / _` | / _ \ | '_ \ 
/\__/ /| (_| || || |_ | |_| || |/ / | |  | (_| || (_| || (_) || | | |
\____/  \__,_||_| \__| \__, ||___/  |_|   \__,_| \__, | \___/ |_| |_|
                       __/ |                     __/ |              
                      |___/                     |___/               
EOF
    echo -e "${NC}"
    echo "========================================"
    echo -e "${YELLOW}aCis Lineage 2 Server Installation Script${NC}"
    echo "========================================"
    echo ""
}

# Function to log messages with timestamp
log_message() {
    local message=$1
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    echo "$timestamp - $message" | tee -a "$TEMP_LOG"
}

# Function to generate random string
generate_random_string() {
    local length=$1
    cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c "$length"
}

# Function to generate random number
generate_random_number() {
    local length=$1
    cat /dev/urandom | tr -dc '0-9' | head -c "$length"
}

# Improved spinner animation that doesn't interfere with text
spinner() {
    local pid=$1
    local timeout=$2
    local message=$3
    local delay=0.1
    local spin_chars=('>>>>    ' ' >>>>   ' '  >>>>  ' '   >>>> ' '    >>>>' '   <<<< ' '  <<<<  ' ' <<<<   ' '<<<<    ' '<<<<<<  ')
    local i=0
    local start_time=$(date +%s)
    
    # Print the initial message
    echo -ne "\r${BLUE}$message${NC} "
    
    while ps -p "$pid" > /dev/null; do
        local current_time=$(date +%s)
        if [ $((current_time - start_time)) -gt "$timeout" ]; then
            kill -TERM "$pid" 2>/dev/null
            log_message "Command timed out after $timeout seconds"
            return 1
        fi
        
        # Print spinner character
        printf "\r${BLUE}$message${NC} [${spin_chars[i]}]"
        i=$(( (i+1) % ${#spin_chars[@]} ))
        sleep $delay
    done
    
    wait "$pid" 2>/dev/null
    local exit_status=$?
    printf "\r${BLUE}$message${NC} [${GREEN}Done${NC}]\n"
    return $exit_status
}

# Function to run command with spinner and logging
run_with_spinner() {
    local message=$1
    local timeout=$2
    shift 2
    log_message "$message"
    # Check if TEMP_LOG is writable
    if [ ! -w "$TEMP_LOG" ]; then
        echo -e "${RED}Error: Log file $TEMP_LOG is not writable.${NC}"
        exit 1
    fi
    # Run command, capturing both stdout and stderr, and append to log
    "$@" >> "$TEMP_LOG" 2>&1 &
    local pid=$!
    if spinner "$pid" "$timeout" "$message"; then
        log_message "$message completed successfully"
    else
        echo -e "${RED}Failed.${NC} Check log for details."
        log_message "$message failed. Exit code: $?"
        # Dump last few lines of log to console for debugging
        echo -e "${YELLOW}Last 5 lines of log:${NC}"
        tail -n 5 "$TEMP_LOG"
        exit 1
    fi
}

# Function to setup user files
setup_user_files() {
    local user=$1
    local home_dir="/home/$user"
    
    # Create credentials file
    local creds_file="$home_dir/acis_credentials.txt"
    touch "$creds_file"
    chown "$user:$user" "$creds_file"
    chmod 600 "$creds_file"
    
    # Update TEMP_LOG to point to final log file but don't move yet
    TEMP_LOG="$home_dir/acis_setup.log"
    touch "$TEMP_LOG"
    chown "$user:$user" "$TEMP_LOG"
    chmod 600 "$TEMP_LOG"
    
    echo "$creds_file"
}

# Function to append to credentials file
append_credential() {
    local cred_file=$1
    local key=$2
    local value=$3
    echo "$key: $value" >> "$cred_file"
    log_message "Appended credential: $key"
}

# Function to get software version
get_software_version() {
    local software=$1
    case $software in
        java)
            java -version 2>&1 | head -n 1 | awk -F '"' '{print $2}'
            ;;
        mariadb)
            mysql --version | awk '{print $5}'
            ;;
        apache2)
            apache2 -v | head -n 1 | awk '{print $3}' | cut -d'/' -f2
            ;;
        php)
            php -v | head -n 1 | awk '{print $2}'
            ;;
        git)
            git --version | awk '{print $3}'
            ;;
        ant)
            ant -version | awk '{print $4}'
            ;;
        unzip)
            unzip -v | head -n 1 | awk '{print $2}'
            ;;
        ufw)
            ufw version | head -n 1 | awk '{print $2}'
            ;;
        fail2ban)
            fail2ban-client version | awk '{print $3}'
            ;;
        *)
            echo "Unknown"
            ;;
    esac
}

# Function to build aCis project
build_acis_project() {
    local user=$1
    local home_dir="/home/$user"
    
    # Build the project
    run_with_spinner "Building aCis project..." 600 \
        su - "$user" -c "cd ~/acis/aCis_gameserver && ant -f build.xml"
    
    # Create build directory
    run_with_spinner "Creating build directory..." 60 \
        su - "$user" -c "mkdir -p ~/aCis-built/gameserver"
    
    # Copy required files
    run_with_spinner "Copying game server files..." 300 \
        bash -c "cp -r $home_dir/acis/aCis_gameserver/build/dist/gameserver/* $home_dir/aCis-built/gameserver/"
    
    run_with_spinner "Copying login server files..." 300 \
        bash -c "cp -r $home_dir/acis/aCis_gameserver/build/dist/login $home_dir/aCis-built/"
    
    # Fix ownership of aCis-built directory
    run_with_spinner "Fixing ownership of aCis-built directory..." 60 \
        bash -c "chown -R $user:$user $home_dir/aCis-built"
    
    run_with_spinner "Copying l2jserver.jar..." 60 \
        bash -c "cp $home_dir/acis/aCis_gameserver/build/l2jserver.jar $home_dir/aCis-built/gameserver/"
    
    # Copy specific folders from data pack
    run_with_spinner "Copying data pack components..." 300 \
        bash -c "mkdir -p $home_dir/aCis-built/gameserver/data && \
                 cp -r $home_dir/acis/aCis_datapack/data $home_dir/aCis-built/gameserver/ && \
                 mkdir -p $home_dir/aCis-built/gameserver/sql && \
                 cp -r $home_dir/acis/aCis_datapack/sql $home_dir/aCis-built/gameserver/ && \
                 mkdir -p $home_dir/aCis-built/gameserver/tools && \
                 cp -r $home_dir/acis/aCis_datapack/tools $home_dir/aCis-built/gameserver/"
    
    # Copy serverNames.xml to login directory
    run_with_spinner "Copying serverNames.xml to login directory..." 60 \
        bash -c "cp $home_dir/aCis-built/gameserver/data/serverNames.xml $home_dir/aCis-built/login/serverNames.xml && \
                 chown $user:$user $home_dir/aCis-built/login/serverNames.xml"
    
    # Download and unpack geodata
    run_with_spinner "Downloading geodata..." 300 \
        bash -c "wget -q -O $home_dir/aCis-built/gameserver/data/geodata/L2OFF_GEO_11-21.zip http://anothercrappyinterludeserver.com/files/geodata/L2OFF%20GEO%20-%2011-21.zip"
    
    run_with_spinner "Unpacking geodata..." 300 \
        bash -c "unzip -o $home_dir/aCis-built/gameserver/data/geodata/L2OFF_GEO_11-21.zip -d $home_dir/aCis-built/gameserver/data/geodata/ && \
                 chown -R $user:$user $home_dir/aCis-built/gameserver/data/geodata"
    
    # Make all .sh files executable
    run_with_spinner "Making .sh files executable..." 60 \
        bash -c "find $home_dir/aCis-built -type f -name \"*.sh\" -exec chmod +x {} \;"
    
    # Final ownership fix
    run_with_spinner "Ensuring final ownership of aCis-built directory..." 60 \
        bash -c "chown -R $user:$user $home_dir/aCis-built"
}

# Function to configure database installer
configure_database_installer() {
    local user=$1
    local db_user=$2
    local db_pass=$3
    local home_dir="/home/$user"
    
    # Configure database_installer.sh
    run_with_spinner "Configuring database installer..." 60 \
        bash -c "sed -i \"s/^LSDB=.*/LSDB='acis_login'/\" $home_dir/aCis-built/gameserver/tools/database_installer.sh && \
                 sed -i \"s/^LSUSER=.*/LSUSER='$db_user'/\" $home_dir/aCis-built/gameserver/tools/database_installer.sh && \
                 sed -i \"s/^LSPASS=.*/LSPASS='$db_pass'/\" $home_dir/aCis-built/gameserver/tools/database_installer.sh && \
                 sed -i \"s/^GSDB=.*/GSDB='acis_gameserver'/\" $home_dir/aCis-built/gameserver/tools/database_installer.sh && \
                 sed -i \"s/^GSUSER=.*/GSUSER='$db_user'/\" $home_dir/aCis-built/gameserver/tools/database_installer.sh && \
                 sed -i \"s/^GSPASS=.*/GSPASS='$db_pass'/\" $home_dir/aCis-built/gameserver/tools/database_installer.sh"
}

# Function to configure login server database properties
configure_login_database() {
    local user=$1
    local db_user=$2
    local db_pass=$3
    local home_dir="/home/$user"
    local config_file="$home_dir/aCis-built/login/config/loginserver.properties"
    local server_ip=$(hostname -I | awk '{print $1}')
    
    run_with_spinner "Configuring login server database properties..." 60 \
        bash -c "sed -i \"s|^URL =.*|URL = jdbc:mariadb://localhost/acis_login|\" $config_file && \
                 sed -i \"s|^Login =.*|Login = $db_user|\" $config_file && \
                 sed -i \"s|^Password =.*|Password = $db_pass|\" $config_file && \
                 sed -i \"s|^Hostname =.*|Hostname = $server_ip|\" $config_file"
}

# Function to configure game server database properties
configure_gameserver_database() {
    local user=$1
    local db_user=$2
    local db_pass=$3
    local home_dir="/home/$user"
    local config_file="$home_dir/aCis-built/gameserver/config/server.properties"
    
    run_with_spinner "Configuring game server database properties..." 60 \
        bash -c "sed -i \"s|^URL =.*|URL = jdbc:mariadb://localhost/acis_gameserver|\" $config_file && \
                 sed -i \"s|^Login =.*|Login = $db_user|\" $config_file && \
                 sed -i \"s|^Password =.*|Password = $db_pass|\" $config_file"
}

# Function to run database installer
run_database_installer() {
    local user=$1
    local home_dir="/home/$user"
    
    run_with_spinner "Making database installer executable..." 60 \
        bash -c "chmod +x $home_dir/aCis-built/gameserver/tools/database_installer.sh"
    
    run_with_spinner "Running database installer..." 300 \
        su - "$user" -c "cd ~/aCis-built/gameserver/tools && echo 'f' | ./database_installer.sh"
}

# Display final information
display_final_info() {
    local cred_file=$1
    
    # Move temp log to final location
    local log_file="/home/$L2_USER/acis_setup.log"
    mv "$TEMP_LOG" "$log_file"
    chown "$L2_USER:$L2_USER" "$log_file"
    chmod 600 "$log_file"
    
    echo -e "\n${YELLOW}=== Installation Complete ===${NC}"
    echo -e "${GREEN}All components installed successfully!${NC}"
    
    # Software Information
    echo -e "\n${YELLOW}=== SOFTWARE INFORMATION ===${NC}"
    echo -e "Java: $(get_software_version java) ${GREEN}(Installed)${NC}"
    echo -e "MariaDB: $(get_software_version mariadb) ${GREEN}(Installed)${NC}"
    echo -e "Apache2: $(get_software_version apache2) ${GREEN}(Installed)${NC}"
    echo -e "PHP: $(get_software_version php) ${GREEN}(Installed)${NC}"
    echo -e "Git: $(get_software_version git) ${GREEN}(Installed)${NC}"
    echo -e "Ant: $(get_software_version ant) ${GREEN}(Installed)${NC}"
    echo -e "Unzip: $(get_software_version unzip) ${GREEN}(Installed)${NC}"
    echo -e "UFW: $(get_software_version ufw) ${GREEN}(Installed)${NC}"
    echo -e "Fail2ban: $(get_software_version fail2ban) ${GREEN}(Installed)${NC}"
    
    # Important Credentials
    echo -e "\n${YELLOW}=== IMPORTANT CREDENTIALS ===${NC}"
    grep -E 'Adminer Login|aCis DB|aCis Server User' "$cred_file"
    
    # Access Information
    echo -e "\n${YELLOW}=== ACCESS INFORMATION ===${NC}"
    echo -e "Adminer URL: ${BLUE}http://$(hostname -I | awk '{print $1}')/adminer${NC}"
    echo -e "aCis Server Location: ${BLUE}/home/$L2_USER/aCis-built${NC}"
    echo -e "Credentials file: ${BLUE}$cred_file${NC}"
    echo -e "Log file: ${BLUE}$log_file${NC}"
    
    # Next Steps
    echo -e "\n${YELLOW}=== NEXT STEPS ===${NC}"
    echo -e "1. Switch to your aCis user: ${BLUE}su - $L2_USER${NC}"
    echo -e "2. Your aCis files are in aCis-built"
    echo -e "3. All that should be left is to configure and register your GameServer."
    echo -e "This script is built and maintained with nostalgia by Drakken of SaltyDragon.io"
    echo -e "If you find a bug or need help go here: ${BLUE}https://acis.i-live.eu/index.php?${NC}"
}

# Main installation process
main() {
    display_header
    
    # Check if running as root
    if [[ $EUID -ne 0 ]]; then
        echo -e "${RED}This script must be run as root. Exiting.${NC}"
        exit 1
    fi

    # Check Ubuntu version
    if ! lsb_release -d | grep -q "Ubuntu 22\|Ubuntu 24"; then
        echo -e "${RED}This script only supports Ubuntu 22 or Ubuntu 24. Exiting.${NC}"
        exit 1
    fi

    # Create aCis-themed user
    ACIS_NAMES=("Aden" "Giran" "Oren" "Dion" "Gludio" "Heine" "Schuttgart")
    L2_USER="${ACIS_NAMES[$RANDOM % ${#ACIS_NAMES[@]}]}$(generate_random_number 3)"
    L2_PASS=$(generate_random_string 10)
    
    run_with_spinner "Creating user $L2_USER..." 60 \
        bash -c "useradd -m -s /bin/bash '$L2_USER' && echo '$L2_USER:$L2_PASS' | chpasswd"
    
    # Setup user files and get credentials file path
    CREDENTIALS_FILE=$(setup_user_files "$L2_USER")
    append_credential "$CREDENTIALS_FILE" "aCis Server User" "$L2_USER"
    append_credential "$CREDENTIALS_FILE" "aCis Server User Password" "$L2_PASS"

    # Update system
    run_with_spinner "Updating system packages..." 600 \
        bash -c "export DEBIAN_FRONTEND=noninteractive; \
                 apt update -qq && apt upgrade -y -qq -o Dpkg::Options::='--force-confold' -o Dpkg::Options::='--force-confdef'"

    # Install Git
    if ! command -v git >/dev/null; then
        run_with_spinner "Installing Git..." 300 \
            bash -c "DEBIAN_FRONTEND=noninteractive apt install -y -qq git"
    else
        log_message "Git is already installed."
    fi

    # Install Java
    JAVA_VERSION=$(java -version 2>&1 | grep -oP 'version "\K[0-9]+' || echo "none")
    if [[ "$JAVA_VERSION" != "21" ]]; then
        run_with_spinner "Installing OpenJDK 21..." 300 \
            bash -c "DEBIAN_FRONTEND=noninteractive apt install -y -qq openjdk-21-jdk"
        run_with_spinner "Setting Java alternatives..." 60 \
            update-java-alternatives -s java-1.21.0-openjdk-amd64
    else
        log_message "Java 21 is already installed."
    fi

    # Install Ant
    if ! command -v ant >/dev/null; then
        run_with_spinner "Installing Ant..." 300 \
            bash -c "DEBIAN_FRONTEND=noninteractive apt install -y -qq ant"
    else
        log_message "Ant is already installed."
    fi

    # Install unzip
    if ! command -v unzip >/dev/null; then
        run_with_spinner "Installing unzip..." 300 \
            bash -c "DEBIAN_FRONTEND=noninteractive apt install -y -qq unzip"
    else
        log_message "unzip is already installed."
    fi

    # Install UFW
    if ! command -v ufw >/dev/null; then
        run_with_spinner "Installing UFW..." 300 \
            bash -c "DEBIAN_FRONTEND=noninteractive apt install -y -qq ufw"
    else
        log_message "UFW is already installed."
    fi

    # Configure UFW
    run_with_spinner "Configuring UFW firewall..." 60 \
        bash -c "ufw allow 22 && ufw allow 80 && ufw allow 2106 && ufw allow 7777 && ufw --force enable"

    # Install fail2ban
    if ! command -v fail2ban-client >/dev/null; then
        run_with_spinner "Installing fail2ban..." 300 \
            bash -c "DEBIAN_FRONTEND=noninteractive apt install -y -qq fail2ban"
    else
        log_message "fail2ban is already installed."
    fi

    # Configure fail2ban for SSH
    run_with_spinner "Configuring fail2ban for SSH..." 60 \
        bash -c "cat > /etc/fail2ban/jail.d/ssh-jail.conf <<EOF
[ssh]
enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 3
EOF
                 systemctl restart fail2ban"

    # Install MariaDB
    if ! command -v mysql >/dev/null && ! command -v mariadb >/dev/null; then
        run_with_spinner "Installing MariaDB..." 300 \
            bash -c "DEBIAN_FRONTEND=noninteractive apt install -y -qq mariadb-server"
        
        # Secure MariaDB
        run_with_spinner "Securing MariaDB..." 60 \
            bash -c "mysql -e \"DELETE FROM mysql.user WHERE User='';\" && \
                     mysql -e \"DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');\" && \
                     mysql -e \"DROP DATABASE IF EXISTS test;\" && \
                     mysql -e \"DELETE FROM mysql.db WHERE Db='test' OR Db='test\\\\_%';\" && \
                     mysql -e \"FLUSH PRIVILEGES;\""
    else
        echo -e "${YELLOW}MySQL or MariaDB is already installed. Skipping database installation.${NC}"
    fi

    # Create aCis databases and user
    ACIS_DB_USER="acisuser$(generate_random_number 3)"
    ACIS_DB_PASS=$(generate_random_string 12)
    
    run_with_spinner "Creating aCis databases..." 60 \
        bash -c "mysql -e \"CREATE DATABASE acis_gameserver;\" && \
                 mysql -e \"CREATE DATABASE acis_login;\" && \
                 mysql -e \"CREATE USER '$ACIS_DB_USER'@'localhost' IDENTIFIED BY '$ACIS_DB_PASS';\" && \
                 mysql -e \"GRANT ALL PRIVILEGES ON acis_gameserver.* TO '$ACIS_DB_USER'@'localhost';\" && \
                 mysql -e \"GRANT ALL PRIVILEGES ON acis_login.* TO '$ACIS_DB_USER'@'localhost';\" && \
                 mysql -e \"FLUSH PRIVILEGES;\""
    
    append_credential "$CREDENTIALS_FILE" "aCis Gameserver DB" "acis_gameserver"
    append_credential "$CREDENTIALS_FILE" "aCis Login DB" "acis_login"
    append_credential "$CREDENTIALS_FILE" "aCis DB User" "$ACIS_DB_USER"
    append_credential "$CREDENTIALS_FILE" "aCis DB Password" "$ACIS_DB_PASS"

    # Install Apache2 with PHP
    if ! command -v apache2 >/dev/null && ! command -v nginx >/dev/null; then
        run_with_spinner "Installing Apache2 with PHP..." 300 \
            bash -c "DEBIAN_FRONTEND=noninteractive apt install -y -qq apache2 php libapache2-mod-php php-mysql"
        systemctl enable apache2 >/dev/null 2>&1
        systemctl start apache2 >/dev/null 2>&1
    else
        echo -e "${YELLOW}Apache2 or Nginx is already installed. Skipping web server installation.${NC}"
    fi

    # Install Adminer
    ADMINER_VERSION="4.8.1"
    run_with_spinner "Installing Adminer..." 60 \
        bash -c "mkdir -p /var/www/html/adminer && \
                 wget -q -O /var/www/html/adminer/index.php https://github.com/vrana/adminer/releases/download/v$ADMINER_VERSION/adminer-$ADMINER_VERSION.php && \
                 chown -R www-data:www-data /var/www/html/adminer && \
                 chmod -R 755 /var/www/html/adminer"
    
    # Create Adminer database user
    ADMINER_USER="adminer$(generate_random_number 3)"
    ADMINER_PASS=$(generate_random_string 12)
    run_with_spinner "Creating Adminer database user..." 60 \
        bash -c "mysql -e \"CREATE USER '$ADMINER_USER'@'localhost' IDENTIFIED BY '$ADMINER_PASS';\" && \
                 mysql -e \"GRANT ALL PRIVILEGES ON *.* TO '$ADMINER_USER'@'localhost' WITH GRANT OPTION;\" && \
                 mysql -e \"FLUSH PRIVILEGES;\""
    
    # Secure Adminer with proper Apache2 authentication
    APACHE_AUTH_USER="adminer"
    APACHE_AUTH_PASS=$(generate_random_string 12)
    run_with_spinner "Securing Adminer..." 60 \
        bash -c "htpasswd -b -c /etc/apache2/.htpasswd '$APACHE_AUTH_USER' '$APACHE_AUTH_PASS' && \
                 cat > /etc/apache2/conf-available/adminer.conf <<EOF
Alias /adminer /var/www/html/adminer

<Directory /var/www/html/adminer>
    AuthType Basic
    AuthName \"Adminer Access\"
    AuthUserFile /etc/apache2/.htpasswd
    Require valid-user
</Directory>
EOF
                 a2enconf adminer >/dev/null && \
                 systemctl reload apache2 >/dev/null"
    
    append_credential "$CREDENTIALS_FILE" "Adminer Login User" "$APACHE_AUTH_USER"
    append_credential "$CREDENTIALS_FILE" "Adminer Login Password" "$APACHE_AUTH_PASS"
    append_credential "$CREDENTIALS_FILE" "Adminer DB User" "$ADMINER_USER"
    append_credential "$CREDENTIALS_FILE" "Adminer DB Password" "$ADMINER_PASS"

    # Clone repository as the aCis user
    echo -e "\n${YELLOW}=== Repository Setup ===${NC}"
    echo "Do you want to use free aCis access or paid aCis access?"
    select ACIS_ACCESS in "free" "paid"; do
        case $ACIS_ACCESS in
            free)
                run_with_spinner "Cloning free aCis repository..." 300 \
                    su - "$L2_USER" -c "git clone https://gitlab.com/Tryskell/acis_public.git ~/acis"
                break
                ;;
            paid)
                read -p "Paid aCis repository URL: " ACIS_URL
                read -p "GitLab username: " GITLAB_USER
                read -s -p "GitLab password/token: " GITLAB_PASS
                echo
                run_with_spinner "Cloning paid aCis repository..." 300 \
                    su - "$L2_USER" -c "git clone https://$GITLAB_USER:$GITLAB_PASS@gitlab.com/${ACIS_URL#https://gitlab.com/} ~/acis"
                break
                ;;
            *)
                echo "Invalid option, please select 1 or 2"
                ;;
        esac
    done

    # Build and prepare aCis server
    build_acis_project "$L2_USER"
    
    # Configure database installer
    configure_database_installer "$L2_USER" "$ACIS_DB_USER" "$ACIS_DB_PASS"
    
    # Configure login and game server database properties
    configure_login_database "$L2_USER" "$ACIS_DB_USER" "$ACIS_DB_PASS"
    configure_gameserver_database "$L2_USER" "$ACIS_DB_USER" "$ACIS_DB_PASS"
    
    # Run database installer
    run_database_installer "$L2_USER"

    # Display final information
    display_final_info "$CREDENTIALS_FILE"
}

main "$@"
